// Ninf-G-XX, ver. 0.1
// Copyright 2011 National Institute of Advanced Industrial Science and Technology
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdio>
#include "NcAPI.h"

int main(int argc, char ** argv){
  double a [2][2] = {{1, 0},{0, 1}};
  double b [2][2] = {{1, 2},{3, 4}};
  double c [2][2] = {{0, 0},{0, 0}};  

  argc = NinfParseArg(argc, argv);
  int code;
  NinfSessionId id1;
  NinfSessionId id2;

  if ((code = NinfCallAsync("test/matmul", &id1, 2, a, b, c)) != NINF_NO_ERROR)
    fprintf(stderr, "something happend: %d\n", code);

  if ((code = NinfCallAsync("test/matmul", &id2, 2, a, b, c)) != NINF_NO_ERROR)
    fprintf(stderr, "something happend: %d\n", code);

  NinfCancel(id1);



  NinfSessionId id;
  if ((code = NinfWaitAny(&id)) != NINF_NO_ERROR)
    fprintf(stderr, "something happend: %d\n", code);    
  else
    fprintf(stderr, "%d done \n", id);

  if ((code = NinfWaitAny(&id)) != NINF_NO_ERROR)
    fprintf(stderr, "something happend: %d\n", code);    
  else 
    fprintf(stderr, "%d done \n", id);

  NcExecInfo info;

  if ((code = NinfGetInfo(id1, &info)) != NINF_NO_ERROR)
    fprintf(stderr, "something happend: %d\n", code);    
  else 
    fprintf(stderr, "%s, %s \n", 
	    NcSessionStatusStrings[info.status], 
	    NinfErrorCodeStrings[info.code]);

  if ((code = NinfGetInfo(id2, &info)) != NINF_NO_ERROR)
    fprintf(stderr, "something happend: %d\n", code);    
  else 
    fprintf(stderr, "%s, %s \n", 
	    NcSessionStatusStrings[info.status], 
	    NinfErrorCodeStrings[info.code]);

  NinfFinalize();

  for (int i = 0; i < 2; i++){
    for (int j = 0; j < 2; j++){
      printf("%f ", c[i][j]);
    }
    printf("\n");
  }
  return 0;
}
